package com.liang.utils;

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Data
@PropertySource(value = "classpath:thirdparty.properties")
@Component
public class AliyunSMS {

    //模板
    @Value("${aliyunx.templateCode}")
    private  String templateCode;
    //签名
//    @Value("${aliyunx.SignName}")
    private  String SignName = "学习规划平台";

    //阿里云AccessKey
    @Value("${aliyunx.AccessKey}")
    private  String AccessKey;

    //阿里云AccessKey的密码
    @Value("${aliyunx.AccessKeyPassword}")
    private  String AccessKeyPassword;

    //阿里云sms-endpoint
    @Value("${aliyunx.endpoint}")
    private  String endpoint;

    /**
     * 使用 AK&ASK 初始化账号 Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception 短信推送异常
     */
    public static Client createClient(String accessKeyId, String accessKeySecret, String endpoint)
            throws Exception {
        Config config =
                new Config()
                        // 必填，您的 AccessKey ID
                        .setAccessKeyId(accessKeyId)
                        // 必填，您的 AccessKey Secret
                        .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        return new Client(config);
    }



    public boolean send(String Phone,String Code) throws Exception{
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS
        Client client = createClient(this.AccessKey, this.AccessKeyPassword, this.endpoint);
        SendSmsRequest sendSmsRequest =
                new SendSmsRequest()
                        .setSignName(this.SignName)
                        .setTemplateCode(this.templateCode)
                        .setPhoneNumbers(Phone)
                        .setTemplateParam("{\"code\":\"" + Code + "\"}");
        try {
            log.info("发送短信入参: " + JSONUtil.toJsonStr(sendSmsRequest));
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            log.info("发送短信结果: " + JSONUtil.toJsonStr(sendSmsResponse));
            if (Objects.nonNull(sendSmsResponse) && sendSmsResponse.getBody().code.equals("OK")) {
                return Boolean.TRUE;
            }
        } catch (TeaException error) {
            // 如有需要，请打印 error
            log.error("短信推送异常结果: " + error.message);
            return Boolean.FALSE;
        } catch (Exception e) {
            TeaException error = new TeaException(e.getMessage(), e);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            log.error("短信推送异常结果: " + error.message);
            return Boolean.FALSE;
        }

        return Boolean.FALSE;
    }


}
